package cn.itcast.bos.web.action.base;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import cn.itcast.bos.service.base.AreaService;
import com.alibaba.fastjson.JSONArray;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Row;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Actions;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

import cn.itcast.bos.domain.base.Area;
@Controller
@Namespace("/")
@Scope("prototype")
@ParentPackage("struts-default")
public class AreaAction  extends ActionSupport implements ModelDriven<Area>{
	private Area area = new Area();
	@Autowired
	private AreaService areaService;
	//接收上传文件
	public File file;
	public  void setFile(File file){
		this.file =file;
	}
	public String fileFileName;
	public String fileContextType;
	@Override
	public Area getModel() {
		// TODO Auto-generated method stub
		return area;
	}
	@Action(value="area_batchImport")
	public String batchImport() throws IOException {
		 System.out.print(fileFileName);
		areaService.saveBatch(file,fileFileName);
		return NONE;
	}
	//分页条件查询
	public  Integer rows;
	public  Integer page;
	@Action(value = "area_pageQuery")
	public String pageQuery() throws IOException {
		ServletActionContext.getResponse().setContentType("text/html;charset=utf-8");
		Pageable pageable = new PageRequest(page -1,rows);
		Page<Area> pageData = areaService.pageQuerry(pageable,area);
		Map<String,Object> map = new HashMap<>();
		map.put("total",pageData.getTotalElements());
		map.put("rows",pageData.getContent());
		String json = JSONArray.toJSONString(map);
		ServletActionContext.getResponse().getWriter().write(json);
		return NONE;
	}
}
